﻿using System;
using QuanLyHopDong.Biz;
using QuanLyHopDong.Biz.Model;

namespace QuanLyHopDong.Web.HopDong
{
    public partial class KhoiLuongVatTuNew : System.Web.UI.Page
    {
        #region Properties

        private KhoiLuongVatTuBiz _khoiLuongVatTuBiz;
        KhoiLuongVatTuBiz KhoiLuongVatTuManager
        {
            get
            {
                return _khoiLuongVatTuBiz ?? (_khoiLuongVatTuBiz = new KhoiLuongVatTuBiz());
            }
        }

        private HopDongBiz _hopDongBiz;
        private HopDongBiz HopDongManager
        {
            get { return _hopDongBiz ?? (_hopDongBiz = new HopDongBiz()); }
        }

        private int _hopDongId;
        private int HopDongId
        {
            get
            {
                if (_hopDongId <= 0)
                    int.TryParse(Request.QueryString["hopDongId"], out _hopDongId);
                return _hopDongId;
            }
        }

        private HopDongView _hopDongView;
        private HopDongView CurrentHopDongView
        {
            get
            {
                if (_hopDongView == null && HopDongId > 0)
                {
                    _hopDongView = HopDongManager.GetById(HopDongId);
                }
                return _hopDongView;
            }
        }

        private KhoiLuongBiz _khoiLuongBiz;
        private KhoiLuongBiz KhoiLuongManager
        {
            get { return _khoiLuongBiz ?? (_khoiLuongBiz = new KhoiLuongBiz()); }
        }

        private PhieuXuatKhoBiz _phieuXuatKhoBiz;

        private PhieuXuatKhoBiz PhieuXuatKhoManager
        {
            get { return _phieuXuatKhoBiz ?? (_phieuXuatKhoBiz = new PhieuXuatKhoBiz()); }
        }

        #endregion

        #region Events

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (HopDongId <= 0)
                {
                    lblErrorMessage.Text = Resources.GlobalStrings.NotExisted;
                    return;
                }
                LoadHopDongData();
                LoadMaTuyen();
                LoadPhieuXuat();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (ddlMaTuyen.Items.Count == 0 || ddlPhieuXuat.Items.Count == 0) return;
            int khoiLuongId, phieuXuatKhoId;
            if (!int.TryParse(ddlMaTuyen.SelectedValue, out khoiLuongId))
                return;
            if (!int.TryParse(ddlPhieuXuat.SelectedValue, out phieuXuatKhoId))
                return;

            lblErrorMessage.Text = string.Empty;

            #region collect inputs and validate

            var khoiLuongVatTu = new Biz.Model.KhoiLuongVatTu
            {
                HopDongId = HopDongId,
                GhiChu = txtGhiChu.Text,
                KhoiLuongId = khoiLuongId,
                PhieuXuatKhoId = phieuXuatKhoId
            };

            var vatTuName = txtVatTu.Text.Trim();
            if (string.IsNullOrEmpty(vatTuName))
            {
                lblErrorMessage.Text = "Chưa nhập vật tư";
                return;
            }
            khoiLuongVatTu.VatTuName = vatTuName;

            int soluong;
            if (txtSoLuong.Text.Trim().Length > 0 && int.TryParse(txtSoLuong.Text.Trim(), out soluong))
            {
                khoiLuongVatTu.SoLuong = soluong;
            }

            khoiLuongVatTu.DonViTinh = int.Parse(ddlDonViTinh.SelectedValue);
            khoiLuongVatTu.BenCap = byte.Parse(ddlBenCap.SelectedValue);

            int thicong;
            if (txtThiCong.Text.Trim().Length > 0 && int.TryParse(txtThiCong.Text.Trim(), out thicong))
            {
                khoiLuongVatTu.ThiCong = thicong;
            }

            int thucte;
            if (txtThucTe.Text.Trim().Length > 0 && int.TryParse(txtThucTe.Text.Trim(), out thucte))
            {
                khoiLuongVatTu.ThucTe = thucte;
            }

            #endregion

            // create vattu
            try
            {
                KhoiLuongVatTuManager.Create(khoiLuongVatTu);
            }
            catch (Exception ex)
            {
                lblErrorMessage.Text = ex.Message;
            }
            
            Response.Redirect(string.Format("KhoiLuong.aspx?hopDongId={0}#tabs-3", HopDongId));
        }

        #endregion

        #region Helpers

        private void LoadMaTuyen()
        {
            var khoiLuongs = KhoiLuongManager.GetKhoiLuongs(HopDongId);
            ddlMaTuyen.DataValueField = "Id";
            ddlMaTuyen.DataTextField = "MaTuyenExt";
            ddlMaTuyen.DataSource = khoiLuongs;
            ddlMaTuyen.DataBind();
        }

        private void LoadPhieuXuat()
        {
            var phieuXuats = PhieuXuatKhoManager.GetPhieuXuatKhos(HopDongId);
            ddlPhieuXuat.DataValueField = "Id";
            ddlPhieuXuat.DataTextField = "SoPhieu";
            ddlPhieuXuat.DataSource = phieuXuats;
            ddlPhieuXuat.DataBind();
        }

        public void LoadHopDongData()
        {
            ltrTenDonVi.Text = CurrentHopDongView.TenDonVi;
            ltrSoHopDong.Text = CurrentHopDongView.SoHopDong;
            ltrLoaiDuAn.Text = CurrentHopDongView.LoaiDuAn;
        }

        #endregion
    }
}